/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package LDAPManager;

import LDAPAPI.AnvilLDAP;
import LDAPAPI.AttributeChange;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
import org.forgerock.opendj.ldap.responses.SearchResultEntry;

/**
 *
 * @author anvil
 */
@WebServlet(name = "ModifyEntry", urlPatterns = {"/ModifyEntry"})
public class ModifyEntry extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            //out.write((String) request.getParameter("entry"));

            HttpSession session = request.getSession();

            AnvilLDAP ldap = (AnvilLDAP) session.getAttribute("ldap");

            try {
                String s = (String) request.getParameter("entry");
                
                if (s != null || !s.equals("")) {
                    JSONObject jsonObject = (JSONObject) JSONSerializer.toJSON(s);
                    
                    SearchResultEntry e = ldap.getEntry(jsonObject.getString("name"));
                    //e.replaceAttribute("mail", "lol@fuck.com");
                    
                    jsonObject.getJSONArray("attributes");
                    
                    out.write("\n\n" + e.toString() + "    KKKKKKKKKKKK    " + jsonObject.toString());
                }
            } catch (Exception e) {
                System.out.println("EXCEPTION");
                e.printStackTrace();
            }
        }
    }
    
    private List<AttributeChange> getChanges(JSONObject j, SearchResultEntry e) {
        ArrayList<AttributeChange> a = new ArrayList<AttributeChange>();
        
        JSONArray ja = j.getJSONArray("attributes");
        
        for(int i = 0; i < ja.size(); i++) {
            JSONObject k = (JSONObject) ja.get(i);
            
        }
        
        return null;
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
